classdef CTM < Processor
  % SeaBird's Cell Thermal Mass function
  % Derived from Processor
  % Constructor signature: ctm = CTM(conn), arg. required
  % TODO: change to arg. optional
  %
  % Author: David Goldsmith, Wash. State Dept. of Ecology, dgol461@ecy.wa.gov
  % Original release date: 9/15/2011

    properties
      % inherited from Processor
      % inData;
      % castDT;
      % scanRate;
      % ParamID;
      % ParamDesc;
      % SBEprec;
      % EquipID;
        DBdata;
        a; b;       % Intermediate parameters to be reused
        dcdT; dT; 
        outData;     % Output
    end
    
    methods
        function ctm = CTM(conn) % Constructor
            ctm = ctm@Processor();
            if nargin 
                ctm.doSubstantive(conn)
            end
        end
        
        function doSubstantive(obj, conn)
            global instHeaderData T mSB
            if nargin-1, obj.getDBdata(conn, 'CTM'); end
            if ~isempty(obj.DBdata)
                alpha = obj.DBdata.CTMalpha{1};
                beta  = 1/obj.DBdata.CTMrecipbeta{1};
                obj.a      = 2 * alpha / (beta / instHeaderData.scanRate + 2);
                obj.b      = 1 - 2 * obj.a / alpha;
                rT = T.outData; 
                if mSB, rT = round2(rT, 1e-4); end
                obj.dcdT   = (1 + 0.006 * (rT - 20)) / 10;
                obj.dT     = diff(rT);
                obj.mainFormula;
            end
        end
        
        function mainFormula(obj)
            global C mSB
            N = numel(C.outData);
            CTM = zeros(N,1);
            for i=2:N
                CTM(i) = -obj.b * CTM(i-1) + obj.a * obj.dcdT(i) * obj.dT(i-1);
            end
            tmp = C.outData;
            if mSB
                tmp = round2(tmp, 1e-6);
                obj.outData.C = round2(tmp + 10*CTM, 1e-6);
            else
                obj.outData.C = tmp + 10*CTM;
            end
        end
        
        function delete(obj) % Destructor
            % Here in case I need it
        end
    end
    
end